from django import forms
from django.contrib.auth.models import User
from captcha.fields import CaptchaField
from .models import CustomUser
from main.models import Files,Comment
from ckeditor.widgets import CKEditorWidget
from django.db import models



class ChangeForm(forms.Form):
    oldpassword = forms.CharField(label="旧密码", widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    password = forms.CharField(label="新密码", widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    password2 = forms.CharField(label="确认新密码", widget=forms.PasswordInput(attrs={'class': 'form-control'}))

    def clean_password2(self):
        password = self.cleaned_data['password']
        password2 = self.cleaned_data['password2']
        if password != password2:
            raise forms.ValidationError("密码不一致")



class LoginForm(forms.Form):
    username = forms.CharField(label="用户名",widget=forms.TextInput(attrs={
        'class':'form-control',
        'placeholder':'请输入用户名/邮箱'
    }))
    password = forms.CharField(label="密码", widget=forms.PasswordInput(attrs={
        'class':'form-control',
        'placeholder': '请输入密码'
    }))


class RegistForm(forms.ModelForm):
    password = forms.CharField(label="确认密码",widget=forms.PasswordInput(attrs={
        'class': 'form-control',
        "placeholder": "请输入密码"
    }))
    password2 = forms.CharField(label="再次确认密码",widget=forms.PasswordInput(attrs={
        'class': 'form-control',
        "placeholder": "请再次输入密码"
    }))
    # 添加验证码字段
    captcha = CaptchaField(label="验证码",)

    class Meta:
        model = CustomUser
        fields = ['email','password']
        widgets ={
            'email':forms.EmailInput(attrs={
                'class':'form-control',
                "placeholder":"请输入邮箱"
            }),

        }
    def clean_email(self):
        email = self.cleaned_data['email']
        if CustomUser.objects.filter(email=email).exists():
            raise forms.ValidationError("邮箱已经存在")
        else:
            return email

    def clean_password2(self):
        password = self.cleaned_data['password']
        password2 = self.cleaned_data['password2']
        if password != password2:
            raise forms.ValidationError("密码不一致")

class FilesForm(forms.ModelForm):
    class Meta:
        model = Files
        fields = ['title',  'content','tags','categorys','download_url']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'content': CKEditorWidget(attrs={'class': 'form-control'}),
            'tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
            'categorys': forms.SelectMultiple(attrs={'class': 'form-control'}),
            'download_url':forms.TextInput(attrs={'class': 'form-control'}),

        }



class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['content']
        widgets = {
            'content': forms.Textarea(attrs={
                'class': 'form-control',
                'rows': 3,
                'placeholder': '请输入您的评论...'
            })
        }
        labels = {
            'content': '评论内容'
        }





